#include "parser3.h"

int parser3(fct t)
{
  int i;
  for(i = 0; i<10; i++)
    if(!strcmp(t[i]->name,"main"))
      return(perserfonction(t,i,int j, monstrlen(t[i]->contenu)));
  return 2;
}

int parserfonction(fct t,int i,int j,int m)
{
    char*corps=malloc(256*sizeof(char));
    corps = monstrcat(corps,t[i]->contenu);
    int k, l = monstrlen(corps);
    char *src = malloc(2*sizeof(char));
    char *str1 = malloc(256*sizeof(char));
    char *str2 = malloc(256*sizeof(char));
    char *str3 = malloc(20*sizeof(char));
    char op;
    nodi *tmp,*tmp1,*tmp2;
    int tmpi1,tmpi2,m;
    int b1, b2,b3=0;

    src = monstrcat(src, corps[j]);

    for(j; j<m; j++)
      {
      
	if (corps[j] == '=')
	  {
	    str1 = '\0';
	    str2 = '\0';
	    tmpi1 = 0;
	    tmpi2 = 0;
	    b1 = 0;
	    b2 = 0;

	    while(corps[j]!=' ')
	      j++;
	    if((corps[j]>= 0)&&(corps[j]<=9))
	      {
		b1++;
		while(!isop(corps[j]))
		  {
		    if(corps[j]==' ')
		      {
			tmpi1 += corps[j];
			tmpi1 *= 10;
		      }
		    j++;
		  }
		tmpi1 /= 10;
	      }
	    else
	      {
		while(!isop(corps[j]))
		  {
		    if(corps[j]!=' ')
		      str1 = monstrcat(str1,corps[j]);
		    j++;
		  }
	      }
	    op = corps[j];

	    if((corps[j]>= 0)&&(corps[j]<=9))
	      {
		b2++;
		while(!isop(corps[j]))
		  {
		    if(corps[j]==' ')
		      {
			tmpi2 += corps[j];
			tmpi2 *= 10;
		      }
		    j++;
		  }
		tmpi2 /= 10;
	      }
	    else
	      {
		while(corps[j]!=';')
		  {
		    if(corp[j]!=' ')
		      str2 = monstrcat(str2,corps[j]);
		    j++;
		  }
	      }
	    if((b1 == 0)&&(b2 == 0))
	      {
		tmp = recherche(t[i],src);
		tmp1 = recherche(t[i],str1);
		tmp2 = recherche(t[i],str2);
		tmp->value = operationvar(tmp1,tmp2,op);
	      }
	    else if((b1 == 1)&&(b2 == 0))
	      {
		tmp = recherche(t[i],src);
		tmp2 = recherche(t[i],str2);
		tmp->value = operationvar(tmpi1,tmp2,op);

	      }
	    else if((b1 == 0)&&(b2 == 1))
	      {
		tmp = recherche(t[i],src);
		tmp1 = recherche(t[i],str1);
		tmp->value = operationvar(tmpi2,tmp1,op);

	      }
	    else
	      {
		tmp = recherche(t[i],src);
		tmp->value = operationvar(tmpi1,tmpi2,op);
	      }
	    src = '\0';
	  }  
        else if(!strcmp (src, "si"))
	  {
	    tmpi1 = checkpar(corps,j);
	    if(estvraie(t[i]->var,recupar(corps,j,tmpi1)))
	      {
		src[0]='\0';
	      }
	    else
	      {
		src[0]='\0';
		j = checkacc(corps,j);
		b3 = 1;
	      }

	  }
        else if(!strcmp (src, "tq"))
	  {
	    tmpi1 = checkpar(corps,j);
	    str1 = recupar(corps,j,tmp1);
	    tmpi1 = checkacc(corps,j);
	    str2 = recupacc(corps,j,tmpi1);
	    while(estvraie(t[i]->var,str1))
	      {
		parserfonction(t,i,j,tmp1);
	      }
	    j = tmpi1;
	  }
        else if((!strcmp (src, "sinon"))&&b3)
	  {
	    b3 = 0;
	  }
        else if(!strcmp (src, "pour"))
	  {

	  }
        else if(!strcmp (src, "ent"))
	  {
	    while(corps[j]!= ';')
	      j++;
	    src[0]='\0';
	  }
        else if(!strcmp (src, "car"))
	  {
	    while(corps[j]!= ';')
	      j++;
	    src[0]='\0';
	  }
        else if(!strcmp (src, "flott"))
	  {
	    while(corps[j]!= ';')
	      j++;
	    src[0]='\0';
	  }
        else if(!strcmp (src, "faire"))
	  {
	  }
        else if(!strcmp (src, "aig"))
	  {
	  }
        else if(!strcmp (src, "cas"))
	  {
	  }
        else if(!strcmp (src, "cour"))
	  {
	    while(corps[j]!= ';')
	      j++;
	    src[0]='\0';
	  }
        else if(!strcmp (src, "vide"))
	  {
	    while(corps[j]!= ';')
	      j++;
	    src[0]='\0';
	  }
        else if(!strcmp (src, "longue"))
	  {
	    while(corps[j]!= ';')
	      j++;
	    src[0]='\0';
	  }
        else if(!strcmp (src, "gflott"))
	  {
	    while(corps[j]!= ';')
	      j++;
	    src[0]='\0';
	  }
        else if(!strcmp (src, "NULE"))
	  {
	    while(corps[j]!= ';')
	      j++;
	    src[0]='\0';
	  }
        else if(!strcmp (src, "ecrire"))
	  {
	    m = 0;
	    tmpi1 = indiceecrire(corps,j,0);
	    tmpi2 = indiceecrire(corps,tmpi1, 1);
	    str1 = mystrsub(corps,j,tmpi1);
	    str2 = mystrsub(corps,tmpi1,tmpi2);

	    for(k = 0; k<tmpi1; k++)
	      {
		if(str1[k] == '%')
		  {
		    str3 = variables(str2,m)
		    m++;
		    k++;
		    if(str1[k]=='d')
		      {
			print("%d",returnint(recherche(t[i]->var,str3)));
		      }
		    else if(str1[k]=='c')
		      {
			print("%c",returnchar(recherche(t[i]->var,str3))));
		      }
		    else if(str1[k]=='f')
		      {
			print("%f",);
		      }
		    else if(str1[k]=='s')
		      {
			print("%s",returnchare(recherche(t[i]->var,str3))));
		      }
		    else
		      {
			printf("%c",str1[k]);
		      }
		  }
	      }
    src[0]='\0';
	  }
        else if(!strcmp (src, "casse"))
	  {
	  }
        else if(!strcmp (src, "defaut"))
	  {
	  }

	if((corps[j]!=' ')||(corps[j]!='\n')|(corps!='\r'))
	  src = monstrcat(src, corps[j]);
      }
    return 1;
}

char *variables(char *str, int i)
{
  char *tmp = malloc(20*sizeof(char));
  int j,k = 0;
  int l = mystrlen(str);
  for(j = 0;(j<l)&&(k<i);j++)
    {
      if(str[j]==',')
	k++;
    }
  for(j;(j<l)&&(str[i]==' ');j++){}
  k = 1;
  for(j;(j<l)&&(k);j++)
    {
      if(checkcaract(str[j]))
	{
	tmp = monstrcat(tmp,str[i]);
	}
      else
	{
	  k--;
	}
    }
  return tmp;
}

int indiceecrire(char *str,int i,int k)
{
  int j=0;
  if(!k)
    {
      for(i;j<2;i++)
	{
	  if(str[i]=='"')
	    j++;
	}
    }
  else
    {
      for(i;str[i]!=';';i++){}
    }
  return i;
}

char *cherchestr(char *str)
{
  int i = 1;
  while(str[i] != '"')
    i++;
  while(str[i]!= ',')
    i++;
}

char *mystrsub(char *str,int i, int j)
{
  int k = 0;
  char *tmp = malloc((j-i+2)*sizeof(char));
  for(i;i<j;i++)
    {
      tmp[k]=str[i];
      k++;
    }
  return tmp;
}

int checkacc(char *corps, int i)
{
  int j = 1;

  for(i;j!=0;i++)
    {
      if(corps[i]=='{')
	{
	  j++;
	}
      else if(corps[i]=='}')
	{
	  j--;
	}
    }

  return i;
}

int checkpar(char *corps, int i)
{
  for(i ; corps[i] != '{';i++){}
  return i--;
}

void *operationvar(int i, nodi tmp2, char op)
{
  int j = returnint(tmp2);
  if(op == '+')
    {
      return((void *)(i+j);)
	}
  else if(op == '*')
    {
      return((void *)(i*j));
    }
  else if(op == '/')
    {
      return((void *)(i/j));
    }
  else if(op == '-')
    {
      return((void *)(i-j));
    }
  else if(op == '%')
    {
      return((void *)(i%j));
    }
}

void *operationvar(nodi tmp1, nodi tmp2, char op)
{
  int i  = returnint(tmp1);
  int j = returnint(tmp2);
  if(op == '+')
    {
      return((void *)(i+j);)
	}
  else if(op == '*')
    {
      return((void *)(i*j));
    }
  else if(op == '/')
    {
      return((void *)(i/j));
    }
  else if(op == '-')
    {
      return((void *)(i-j));
    }
  else if(op == '%')
    {
      return((void *)(i%j));
    }
}

int isop(char a)
{
  return ((a == '+')||(a == '-')||(a == '/')||(a == '*')||(a == '%'));
}
